package com.example.linyiquan.application.util;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

/**
 * Created by linyiquan on 17-4-20.
 */

public class MyVolley extends Volley{

    public static RequestQueue requestQueue;

    public interface OnResultLitener{
        void done(JSONObject result);
        void fail(VolleyError error);
    }

    public static void init(Context context){
        requestQueue=Volley.newRequestQueue(context);
    }

    public static void post(String url,JSONObject params,final OnResultLitener litener){
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                litener.done(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                litener.fail(error);
            }
        });

        requestQueue.add(jsonObjectRequest);
        requestQueue.start();
    }

    public static void get(String url,JSONObject params,final OnResultLitener litener){
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                litener.done(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                litener.fail(error);
            }
        });

        requestQueue.add(jsonObjectRequest);
        requestQueue.start();
    }

}
